package com.repeat.member.controller;

import com.api.openfeign.client.UserClient;
import com.api.openfeign.dto.UserDto;
import com.repeat.member.mq.produc.UserPointsProduct;
import com.repeat.member.mq.produc.UserProduct;
import com.repeat.member.service.UserDetailsServiceImpl;
import com.shop.common.constant.RabbitFinalConstant;
import com.shop.common.enums.ExceptionEnum;
import com.shop.common.exception.OperationException;
import com.shop.common.response.ExceptionResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("member")
public class UserController {
    @Autowired
    private UserDetailsServiceImpl userDetailsService;
    @Autowired
    private UserProduct userProduct;

    @Autowired
    private UserClient userClient;

    @GetMapping("info")
    public String info() throws Exception {
        UserDto userDto =  userClient.username("a12345");
        if(userDto == null){
            throw new OperationException(ExceptionEnum.SERVICE_UNAVAILABLE);
        }
        System.out.println(userDto);
        return "info";
    }


    @PostMapping("checkIn")
    public ResponseEntity<?> checkIn() {
        userDetailsService.checkIn();
        return ResponseEntity.ok(ExceptionResponse.Success("签到成功"));
    }
    @Autowired
    private UserPointsProduct userPointsProduct;
    @RequestMapping("msg")
    public String msg(String key,String message) throws InterruptedException {
        if(message.isEmpty()){
            return "没有消息";
        }

        Map<String,Object> map = new HashMap<>();
        map.put("user_id",2);
        map.put("points",200);

        userPointsProduct.SendPointsMsg(RabbitFinalConstant.POINTS_EXCHANGE,RabbitFinalConstant.SING_ROUTING_KEY,map);
        //发送广播消息
//        userProduct.FanoutProduce(message);
        //发送路由
        //userProduct.DirectProduce(key,message);

        //String register_key = RabbitFinalConstant.REGISTER_ROUTING_KEY;

//        userPointsProduct.SendPointsMsg(register_exchange,key,message);

        //userProduct.DirectProduce(key,message);
        return "消息";
    }
}
